package msr.frontend;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import msr.object.Product;
import msr.service.OracleService;
import msr.util.RequestAction;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.*;

@Controller
@SessionAttributes("logged")
@RequestMapping("/product")
public class SearchController {

	private List<Product> rtList = new ArrayList<Product>();
	private OracleService oService = new OracleService();

	// private String username;
	// private String passwd;

	/**
	 * default function when client send request
	 * */
	@RequestMapping("/search.htm")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		System.out.println("load search page....");
		rtList = oService.searchProduct(RequestAction.PRODUCT_ALL,
				RequestAction.GET_ALL_PRODUCT);
		mav.addObject("rtList", rtList);
		mav.setViewName("search");
		return mav;
	}

	@RequestMapping("/getlist.htm")
	public ModelAndView getListProduct(@RequestParam String productname) {
		ModelAndView mav = new ModelAndView();

		System.out.println("search product ...");
		if (productname.equals("")) {
			System.out.println("search all product");
			rtList = oService.searchProduct(RequestAction.PRODUCT_ALL,
					RequestAction.GET_ALL_PRODUCT);
		} else {
			System.out.println("search product by name " + productname);
			rtList = oService.searchProduct(productname,
					RequestAction.FEED_NAME);
		}
		if (rtList == null || rtList.size() == 0) {
			// addActionError(getText("error.search"));
		} else {
			mav.addObject("rtList", rtList);
			mav.setViewName("search");

		}
		return mav;
	}

}
